/*
* @Author: dengjiayao
* @Date:   2017-08-28 16:57:27
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-10-24 11:02:56
*/
import Pages from '../../component/pages/pages.vue'
import {
  showMsg,
  getAnnouncementList,
  deleteAnnouncement
} from '../../vuex/action'

export default {
  name: 'announcement-list',
  components: {
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      filter: {
        market: '1'
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.announcementListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getAnnouncementList,
      xDelete: deleteAnnouncement
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({ start = this.pages.start, limit = this.pages.limit }) {
      this.xGetList({
        start: start,
        limit: limit,
        market: this.filter.market
      })
        .then(data => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.count / limit)
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateItem({
      id,
      market,
      position = '0',
      content,
      url,
      type = '0',
      validBeginTime,
      validEndTime
    }) {
      let ref = this.$root.$refs.announcementConfig
      ref.actionType = 'UPDATE'
      ref.form = {
        id: id,
        market: market,
        position: position,
        content: content,
        url: url,
        type: type,
        validBeginTime: validBeginTime,
        validEndTime: validEndTime
      }
      ref.showModal = true
    },
    swapMarket(market) {
      this.filter.market = market
      this.getList({})
    },
    deleteItem(id) {
      this.xDelete({
        id: id
      }).catch(res => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
